package my.twitter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.webkit.CacheManager;
import android.webkit.ConsoleMessage;
import android.webkit.CookieManager;
import android.webkit.HttpAuthHandler;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(getWebView());
    }
    
    private WebView getWebView(){
    	WebView wv = new WebView(this);
		wv.loadUrl("http://www.twitter.com");
		String ua = wv.getSettings().getUserAgentString();
		Log.e("mytwitter",ua);
		CacheManager.startCacheTransaction();
		CookieManager.getInstance().setAcceptCookie(true);
		wv.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				return false;
			}
			
			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {

			}
			
			@Override
			public void onPageFinished(WebView view, String url) {

			}
			
			@Override
			public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
			
			}
			
			@Override
			public void onReceivedHttpAuthRequest(WebView view, final HttpAuthHandler handler, final String host, final String realm){
			
			}
			
	        /**
	         * Displays SSL error(s) dialog to the user.
	         */
	        @Override
	        public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
	        }
			
	        /**
	         * Check with the user if it is ok to resend POST data as the page they
	         * are trying to navigate to is the result of a POST.
	         */
	        @Override
	        public void onFormResubmission(WebView view, final Message dontResend,
	                                       final Message resend) {
	        	if(null != dontResend) {
	        		dontResend.sendToTarget();
	        	}
	        }


			@Override
			public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
			}
		});
		
		wv.setWebChromeClient(new WebChromeClient() {
			public void onProgressChanged(WebView view, int newProgress) {
				setProgress(newProgress*100);
			}

			public void onReceivedTitle(WebView view, String title) {
			}

			public void onReceivedIcon(WebView view, Bitmap icon) {
			}
			
	        /* Adds a JavaScript error message to the system log and if the JS
	         * console is enabled in the about:debug options, to that console
	         * also.
	         * @param consoleMessage the message object.
	         */
	        @Override
	        public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
	            String message = "Console: " + consoleMessage.message() + " "
	                    + consoleMessage.sourceId() +  ":"
	                    + consoleMessage.lineNumber();

	            Log.e("MyTwitter", message);
	            return true;
	        }
		});
		
		WebSettings webSettings = wv.getSettings();  
		webSettings.setJavaScriptEnabled(true);  
		return wv;
    }
    
}